package com.cjack.frenchexcel.controller;

import com.cjack.frenchexcel.config.AjaxResult;
import com.cjack.frenchexcel.config.CommonConfig;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.model.AdminUserModel;
import com.cjack.frenchexcel.service.RoleService;
import com.cjack.frenchexcel.service.UserService;
import com.cjack.frenchexcel.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping(value = "/adminUser")
public class UserEndpoint {

    @Autowired
    UserService service;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = "/regist", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult regist(AdminUserModel model) {
        model.setCreateTime(new Date());
        model.setRole(roleService.get(model.getRoleId()));
        model.setPasswordStatus("USERCUSTOMER");
        service.register(model);
        return AjaxResult.SUCCESS("注册成功，等管理员审核成功之后即可登录系统");
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult login(HttpSession session,
                            @RequestParam("jobNumber") String jobNumber,
                            @RequestParam("password") String password) {
        AdminUserModel model = service.login(jobNumber, password);
        session.setAttribute(CommonConfig.SESSION_NAME, model.getId());
        return AjaxResult.SUCCESS();
    }

    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult logout(HttpSession session) {
        Object adminUserId = session.getAttribute(CommonConfig.SESSION_NAME);
        AssertUtil.assertNotNull(adminUserId, ErrorCodeDefine.USER_NOT_LOGIN, ErrorCodeDefine.USER_NOT_LOGIN_MSG);
        session.removeAttribute(CommonConfig.SESSION_NAME);
        return AjaxResult.SUCCESS("登出成功");
    }

    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult updateUserInfo(AdminUserModel model) {
        service.updateUserInfo(model);
        return AjaxResult.SUCCESS("修改信息成功!");
    }

    @RequestMapping(value = "/changePassword", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult changePassword(@RequestParam("oldPassword") String oldPassword,
                                     @RequestParam("newPassword") String newPassword,
                                     HttpSession session) {
        Object adminUserId = session.getAttribute(CommonConfig.SESSION_NAME);
        AssertUtil.assertNotNull(adminUserId, ErrorCodeDefine.USER_NOT_LOGIN, ErrorCodeDefine.USER_NOT_LOGIN_MSG);
        service.changePassword((Long) adminUserId, oldPassword, newPassword);
        return AjaxResult.SUCCESS("修改密码成功!");
    }

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public AjaxResult getUserInfo(HttpSession session) {
        Object adminUserId = session.getAttribute(CommonConfig.SESSION_NAME);
        AssertUtil.assertNotNull(adminUserId, ErrorCodeDefine.USER_NOT_LOGIN, ErrorCodeDefine.USER_NOT_LOGIN_MSG);
        AdminUserModel model = service.getUserInfo((Long) adminUserId);
        return AjaxResult.SUCCESS(model);
    }
}
